<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
class BannerTable extends AbstractTable
{
    protected $table = 'hy_banner';
    protected $id = 'banner_id';
    
    
    public function updateItem($id,$data){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        $item = array(
                'type'=>$data['type'],
                'title'=>$data['title'],
                'url'=>$data["url"],
                'update_time'=>new Expression('NOW()'),
        );
        if(isset($data['image'])){
            $item['image'] = $data['image'];
        }
        return $this->update($item,array('banner_id'=>$id));
    }
    
    public function deleteItem($id){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        return $this->delete(array('banner_id'=>$id));
        
    }
    
    public function addItem($data){
        
        $data = array(
            'type'=>$data['type'],
            'title'=>$data['title'],
            'image'=>$data['image'],
            'url'=>$data["url"],
            'create_time'=>new Expression('NOW()'),
            'update_time'=>new Expression('NOW()'),
        );
        
        $this->insert($data);
        $id = $this->lastInsertValue;

    }
    
    public function getItem($id){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        $sql='SELECT * FROM `hy_banner` WHERE `banner_id` =:banner_id';
        $statement=$this->getAdapter()->query($sql);
        $results=$statement->execute(array(':banner_id'=>2));
        $row=$results->current();
        return $row;
    }
    /**
     * 获取banner信息
     * @param array $query
     */
    public function getBannerList($query = array())
    {
        $select = new Select(array('b' =>$this->getTable()));
        if (!empty($query['type'])) {
            $select->where(array('b.type'=>$query['type']));
        }
         
        $rowset = $this->selectWith($select);
        return $rowset->toArray();
    }
    /**
     * 删除banner信息
     * @param unknown $id
     * @throws Exception
     */
    public function deleteByType($query = array()){
        if(empty($query['type'])){
            throw new \Exception("type field is invalid!");
        }
        return $this->delete(array('type'=>$query['type']));
    
    }
}